<?php

class ProductAllController extends BaseController
{

	/**
	 * 所有产品
	 */
	public function actionAdmin()
	{
		$model=new Product('search');
		$model->unsetAttributes();  // clear any default values
		$model->zp_state = array(1,2);//状态不为删除
		if(isset($_GET['Product'])){
			$model->attributes=$_GET['Product'];
			if($_GET['Product']['cateid3']>0){
				$model->catgory_id = (int)$_GET['Product']['cateid3'];
			}elseif($_GET['Product']['cateid2']>0){
				$model->catgory_id = (int)$_GET['Product']['cateid2'];
			}elseif($_GET['Product']['cateid1']>0){
				$model->catgory_id = (int)$_GET['Product']['cateid1'];
			}
		}

		$this->render('admin',array(
			'model'=>$model,
		));
	}



	
	/**
	 * 单条，批量审核通过
	 */
	public function actionCheckPass(){
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zp_id in ($id)");
		//修改内容
		$set = array('zp_status'=>'2');
		//执行操作
		if(Product::model()->updateAll($set,$criteria)){
			
			$product = Product::model()->findAll($criteria);
			foreach($product as $val){
				//判断系统设计，是否需要发送邮件
				if(F::setting("product_check_sent_mail")==1){
					//发送邮件
					EmailTemplates::model()->sentMail($val->zm_id,9,array('message'=>"你发布的产品：".$val->zp_name."审核通过！"));
				}
				//发送消息
				MsgLog::sendMemberInfo($val->zm_id,$message);
			}
			
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}else{
			//失败跳转
			$this->render('../index/error',array('message'=>'对不起，产品审核操作失败，请稍后再试！'));
			exit;
		}
	}
	
	/**
	 *单条，批量下架
	 */
	public function actionDownShelf(){
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zp_id in ($id)");
		//修改内容
		$set = array('zp_state'=>'1');
		//执行操作
		if(Product::model()->updateAll($set,$criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}else{
			//失败跳转
			$this->render('../index/error',array('message'=>'对不起，宝贝下架失败，请稍后再试！'));
			exit;
		}
	}
}
